package com.qf.service.impl;

import com.qf.api.CacheFeign;
import com.qf.constant.CacheConstants;
import com.qf.constant.OperatorConstants;
import com.qf.model.StandardSubmit;
import com.qf.service.StrategyService;
import com.qf.util.CheckMobileOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 号段补全
 *
 * @Author ztf
 * @Date 19:45 2021/11/24
 **/
@Service(value = "phase")
@Slf4j
public class PhaseServiceImpl implements StrategyService {

    @Autowired
    private CacheFeign cacheFeign;

    /**
     * 策略方法
     *
     * @param standardSubmit
     * @return
     */
    @Override
    public boolean execute(StandardSubmit standardSubmit) {
        log.info("【策略模块-号段补全策略】，短信对象内容为：standardSubmit={}", standardSubmit);
        //1. 获取短信发送的目标手机号
        String destMobile = standardSubmit.getDestMobile();
        //2. 根据手机号判断手机号的运营商
        if (CheckMobileOperator.isChinaMobilePhoneNum(destMobile)) {
            //中国移动
            standardSubmit.setOperatorId(OperatorConstants.CHINA_MOBILE);
            standardSubmit.setGatewayID(OperatorConstants.CHINA_MOBILE);
        } else if (CheckMobileOperator.isChinaTelecomPhoneNum(destMobile)) {
            //中国电信
            standardSubmit.setOperatorId(OperatorConstants.CHINA_TELECOM);
            standardSubmit.setGatewayID(OperatorConstants.CHINA_TELECOM);
        } else if (CheckMobileOperator.isChinaUnicomPhoneNum(destMobile)) {
            //中国联通
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNICOM);
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNICOM);
        } else {
            //未知号码
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNKNOWN);
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNKNOWN);
        }
        //3. 获取目标手机号的前七位
        String mobileSubStr = destMobile.substring(0, 7);
        //4. 根据手机号前七位判断归属地
        String proviceAndCity = cacheFeign.get(CacheConstants.CACHE_PREFIX_PHASE + mobileSubStr);

        if (proviceAndCity == null) {
            log.info("【策略模块-号段补全策略】，号码归属地查询失败，destMobile={}", destMobile);
            return false;
        }
        String[] proviceAndCityArray = proviceAndCity.split(",");
        if (proviceAndCityArray.length < 2) {
            log.info("【策略模块-号段补全策略】，号码归属地查询失败，destMobile={}", destMobile);
        }
        standardSubmit.setProvinceId(Integer.parseInt(proviceAndCityArray[0]));
        standardSubmit.setCityId(Integer.parseInt(proviceAndCityArray[1]));
        //5. 获取通道数据
        Map<String, String> channel = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_SMS_CHANNEL + standardSubmit.getOperatorId() + ":" + standardSubmit.getGatewayID());
        //6. 获取路由数据
        Map<String, String> routers = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_ROUTER + standardSubmit.getClientID() + ":" + standardSubmit.getOperatorId());
        //7. 补全短信对象中的下发源号码
        String spnumber = channel.get("spnumber");
        String extendnumber = routers.get("extendnumber");
        standardSubmit.setSrcNumber(spnumber + extendnumber);
        //8. 根据运营商不同，补全这条短信的费用
        String price = routers.get("price");
        standardSubmit.setFee(Long.parseLong(price));
        log.info("【策略模块 - 号段补全策略】，成功执行完毕，standardSubmit={}",standardSubmit);
        return true;
    }
}
